Skip to content

fix(instagram): request the saved feed like the web app, never retry "fail" answers - #4

Open
Pacman1984 wants to merge 4 commits into
masterfrom
fix/instagram-saved-feed-web
Open

Pacman1984 wants to merge 4 commits into
masterfrom
fix/instagram-saved-feed-web

Conversation

@Pacman1984

@Pacman1984 Pacman1984 commented Oct 26, 2026 •

Copy link
Copy Markdown
Owner

Summary

Live test after #3 (2026-10-26 22:02): the new session check passed (session valid, no 401, no password login), but the saved feed on the private API i.instagram.com/api/v1/feed/saved/posts/ answered 200 OK - "fail" status, "We're sorry, but something went wrong." (instaloader retried it 3×).

The session is a web session, so the feed is now requested the way the Instagram web app (and gallery-dl's InstagramRestAPI) do it: www.instagram.com/api/v1/feed/saved/posts/ with the web REST headers.

Changes

  • Web saved feed (instagram.feed_api = "web", default): _WebFeed on a temporary copy_session() (main session unchanged), through context.get_json(host="www.instagram.com") so instaloader's rate controller and error handling stay. Headers: X-IG-App-ID, X-ASBD-ID, X-CSRFToken (from the csrftoken cookie per request), X-IG-WWW-Claim (tracked from x-ig-set-www-claim), X-Requested-With, Sec-Fetch-*, saved page as Referer; count=50, max_id pagination. The iPhone variant stays available (feed_api = "iphone").
  • No retries on "fail": a feed page gets one attempt; only requests transport errors are retried (up to max_connection_attempts). A "fail" answer raises the new InstagramFeedUnavailable (stop reason feed_unavailable, exit code 3, API job error code), saying the session is fine, nothing was lost, and which feed_api to try.
  • Visible session outcome on the console: "Instagram session verified (no password login)", or a warning when the password login is used (with the reason).
  • config validate checks feed_api; docs (operations, configuration, CLI/API reference), CHANGELOG, config.example.toml.

Test plan

  • uv run pytest: 1024 passed, 4 skipped (was 994/4); real InstaloaderContext with faked requests for host/path/params/headers (CSRF from cookie, www-claim carried to page 2), web never touches the i.instagram.com feed and vice versa, pagination/early stop/cursor loop/empty page/item shapes, "fail" = exactly one request + exit 3 + progress kept, network-error retries, config parsing, session console lines, runner/service mapping
  • ruff check, ruff format --check, mypy src clean
  • Live: postparse extract instagram --limit 1 --stop-after-known 1 (2 requests) after merge

Update: no automatic password login (920c6c0)

Live test 22:24 from this branch: the session check said "not logged in", the collector logged in with the password automatically (third scripted password login of the day), and the first web feed page was answered with a confirmation request / login redirect. Decision: password logins are never automatic.

  • InstagramCollector(allow_password_login=False) by default: no session file / unreadable / logged out -> InstagramAuthRequired (exit 2) naming the reason and postparse check instagram --login. Only check instagram --login (interactive) passes True; extract, transcription re-fetches and API jobs force False.
  • Instagram's short reason (login_required, checkpoint_required, redirect target as scheme+host+path) is shown in CLI and job errors; query strings, cookies and tokens are redacted, max 200 chars.
  • Markers are no longer matched inside URLs (a cursor like max_id=...3429... no longer reads as HTTP 429).
  • Tests: 1073 passed, 4 skipped; ruff, mypy clean.

Update: verify after the password login, import the browser session (69792db)

Live tests 2026-10-27 20:07 and 22:16 from this branch: both stopped locally with "session file has no login cookies" (0 requests to Instagram, no password login). The user had run check instagram --login in between, which reported "session is valid" and saved a session without sessionid. Cause: instaloader 4.15.5's password login seeds empty placeholder cookies and reports success on authenticated: true even when no real sessionid comes back (instaloader#2487); --login never re-checked the new session.

  • Password login is verified before saving: placeholders (empty value, no domain) are dropped, a session without sessionid / numeric ds_user_id raises InstagramAuthRequired without a request ("Instagram accepted the password but returned no session cookie (instaloader issue #2487); nothing was saved"), otherwise one session check runs. Nothing is saved on any failure; a throttled check after the login says so (InstagramSessionCheckFailed(after=...), exit 3).
  • New postparse check instagram --import-session: reads the browser's sessionid cookie from a hidden prompt (csrftoken optional, generated when empty), normalizes : to %3A, derives ds_user_id, verifies with exactly one request and saves only a confirmed session of INSTAGRAM_USERNAME. Never uses the password; needs a TTY; mutually exclusive with --login. Values are never printed or logged (tests assert it; the sanitizer also redacts JSON-style and bare session ids).
  • Session check over the web API when feed_api = "web": GET www.instagram.com/api/v1/users/<id>/info/ with the same web headers as the feed (_WebFeed → shared _WebClient), so a browser session is never sent with iPad headers to i.instagram.com. feed_api = "iphone" keeps get_iphone_json.
  • Messages, hints and docs recommend --import-session first, --login second.
  • Tests: 1122 passed, 4 skipped; ruff, format, mypy clean (reviewed and re-run by the orchestrator).
  • Not verified live: that www.instagram.com/api/v1/users/<id>/info/ answers like i.instagram.com, and that a browser sessionid with a generated csrftoken is accepted.

Update: web session check = first saved-feed page, reused by the walk (8e62e0c)

Live 2026-10-28 ~20:30: the user ran check instagram --import-session; its only request, GET www.instagram.com/api/v1/users/<id>/info/, got 429 Too Many Requests (~22 h after the previous Instagram request). Nothing was saved. The web app does not normally call that endpoint.

  • With feed_api = "web" the session check now requests the first page of the saved feed (api/v1/feed/saved/posts/, same params and web headers as the walk, Referer = the saved page) — the request the web app itself makes when the saved page is opened.
  • The page is kept for the walk (tied to the current session, used once), so extract instagram --limit 1 --stop-after-known 1 with a valid session is exactly one request; later pages use its max_id. The import is one request too.
  • A 200 "fail" on the check raises InstagramFeedUnavailable; an answer that is not a feed page raises InstagramSessionCheckFailed; logout/checkpoint/throttle mapping unchanged. Nothing is saved on any failure.
  • Trade-off: the web feed does not name the viewer, so in web mode the account is the one the sessionid belongs to (no username comparison); feed_api = "iphone" keeps the users/<id>/info/ check with the comparison.
  • Tests: 1142 passed, 4 skipped; ruff, format, mypy clean (re-run by the orchestrator).
  • Not verified live: Instagram's answer to the web saved-feed check for an imported browser session, and X-IG-WWW-Claim from the check's page on page 2.

🤖 Generated with Claude Code

Pacman1984 and others added 4 commits October 26, 2026 22:21
…"fail" answers

With a web session, the private-API saved feed on i.instagram.com (iPad
headers) was answered with 200 "fail" "We're sorry, but something went
wrong" and instaloader retried it three times. The feed is now requested
the way the Instagram web app (and gallery-dl's REST client) does it:
GET https://www.instagram.com/api/v1/feed/saved/posts/ with X-IG-App-ID,
X-CSRFToken from the csrftoken cookie, X-Requested-With, X-ASBD-ID,
X-IG-WWW-Claim, Sec-Fetch-* and the saved page as Referer, on a temporary
copy of instaloader's session (cookies, rate controller, error handling).

- New option instagram.feed_api = "web" | "iphone" (default "web");
  invalid values fail with a clear message and in `config validate`.
- Feed pages get a single attempt; only network errors are retried up to
  max_connection_attempts. A "fail" answer raises the new
  InstagramFeedUnavailable (RateLimited subclass): exit code 3, report
  stopped_reason and job error_code "feed_unavailable", message says the
  session is fine, nothing was lost and which feed_api to try.
- The session outcome is reported through the progress callback and
  printed by the CLI ("Instagram session verified (no password login)" or
  a warning before every password login, with the reason).
- Docs (operations, configuration, CLI/API references), changelog and
  config.example.toml updated; tests cover both APIs with fakes and with a
  real InstaloaderContext over a faked HTTP layer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…stagram's reason

A scripted password login right after a session check made Instagram flag
the account (confirmation request / login redirect on the first saved-feed
page). Automatic password logins are now switched off:

- InstagramCollector gets allow_password_login (default False). Without a
  usable session (no session file, unreadable file, session logged out) it
  raises InstagramAuthRequired (exit code 2) that says no password login was
  attempted, why, and to run `postparse check instagram --login` in a
  terminal. The session file is not touched.
- Only `check instagram --login` passes allow_password_login=True and prints
  a warning before the password login. ExtractionService (CLI extract, API
  jobs) and TranscriptionService re-fetches force it to False, even if
  overrides try to enable it.
- Logged-out sessions, confirmations, feed refusals and rate limits quote
  Instagram's sanitized short reason (message "...", or a login redirect as
  scheme://host/path); no query strings, cookies, tokens or bodies, at most
  200 characters. A failed session check no longer quotes the request URL.
- Rate-limit markers are no longer matched inside URLs or request paths, so
  a "fail" answer whose cursor contains 429 is feed_unavailable; HTTP 429 /
  TooManyRequestsException is still a rate limit.

Docs (operations, CLI/API reference, configuration) and CHANGELOG updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… browser session

instaloader's password login (4.15.5) seeds a fresh cookie jar with empty
placeholder cookies (sessionid, ds_user_id, csrftoken, ...) and reports
success as soon as Instagram answers "authenticated: true", even when no
real sessionid cookie came back (instaloader issue #2487). `check instagram
--login` then said "session is valid" and saved a session file without login
cookies, which the next extract rejected.

- After the password login (and the 2FA code) the placeholders are removed,
  a session without sessionid / numeric ds_user_id raises
  InstagramAuthRequired without a request, and the new session is verified
  with one session check before it is saved. Nothing is saved on any failure.
- New `postparse check instagram --import-session`: reads the sessionid
  cookie of the logged-in browser from a hidden prompt (csrftoken optional,
  generated when empty), normalizes it, verifies it with exactly one request
  and saves it only when Instagram confirms the session of INSTAGRAM_USERNAME
  (InstagramCollector.import_session). Never uses the password.
- With feed_api = "web" the session check now asks www.instagram.com with the
  same web headers as the saved feed (one shared web client), so a browser
  session is never sent with iPad headers to i.instagram.com.
- Messages and docs recommend --import-session first, --login as the
  alternative; cookie values in JSON or as bare values are redacted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nd reuse it

With instagram.feed_api = "web" the session check asked
www.instagram.com/api/v1/users/<ds_user_id>/info/. The Instagram web app
does not normally call that endpoint, and a fresh --import-session (its
only request, about 22 hours after the previous one) was answered there
with 429 Too Many Requests.

The web check now requests the first page of the saved feed
(api/v1/feed/saved/posts/), exactly as the feed walk does and as the web
app does when the saved page is opened: same _WebClient and headers,
the saved page as Referer, a single connection attempt. Logged out,
checkpoint and throttle answers map as before; a 200 answer with
"status": "fail" raises InstagramFeedUnavailable instead of being taken
for a logout. Nothing is saved on any failure. The feed does not name
the viewer, so in web mode the account is the one the sessionid belongs
to; feed_api = "iphone" keeps the user-info check and the username
comparison. The unused web user-info path is gone.

The checked page is kept on the collector (tied to the loader's current
session, used once) and the default saved-feed walk starts with it, so
page 1 is never requested twice: an import is one request, and
extract instagram --limit 1 --stop-after-known 1 with a valid session is
exactly one request in web mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant